package com.qdxwx.manage.controller.user;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.member.MemberSaleService;
import com.qdxwx.data.service.order.OrderService;
import com.qdxwx.data.service.user.UserLetterService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.enums.LetterTemplate;
import com.qdxwx.models.member.MemberSale;
import com.qdxwx.models.user.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户(前台用户)Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/user/member")
public class UserMemberController extends BaseController {

    @Autowired
    private UserLetterService userLetterService;
    @Autowired
    private MemberSaleService memberSaleService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;


    /***
     * 赠送用户会员
     */
    @GetMapping("/list.html")
    public String MemberList(HttpServletRequest request, ModelMap mmap) {
        mmap.put("userId", TKConvert.toLong(request.getParameter("userId")));
        return "user/member-give-list";
    }

    /***
     * 赠送用户会员列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo MemberList(HttpServletRequest request) {
        MemberSale member = new MemberSale();
        member.setName(TKString.queryKey(request.getParameter("name")));
        member.setStatus(0);
        List<MemberSale> list = memberSaleService.getList(member, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(memberSaleService.count(member));
        return rspData;
    }

    /**
     * 赠送员会操作
     */
    @Permissions("user:member:free")
    @Log(title = "赠送用户会员", businessType = BusinessType.UPDATE)
    @RequestMapping("/free.json")
    @ResponseBody
    public AjaxResult freeMember(HttpServletRequest request) {
        long userId = TKConvert.toLong(request.getParameter("userId"));
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (userId < 1 || id < 1) return AjaxResult.error("参数错误");
        User user = userService.get(userId);
        if (user == null) {
            return AjaxResult.error("找不到指定用户");
        }
        try {
            MemberSale memberSale = memberSaleService.get(id);
            if (memberSale == null){
                return AjaxResult.error("找不到指定会员商品");
            }
            orderService.addFreeMember(user, memberSale);
            //发送站内信
            userLetterService.send(userId, LetterTemplate.MEMBER_GIVE, memberSale.getName());
            return AjaxResult.success("赠送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统异常");
        }
    }
}
